<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>3.1 Application | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c03/s02.html" />
    
    
    <link rel="prev" href="../c03/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.1"
        data-chapter-title="3.1 Application"
        data-filepath="c03/s01.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="31-application">3.1 Application</h1>
<h2 id="settings">settings</h2>
<p>&#x524D;&#x9762;&#x7684;&#x5B66;&#x4E60;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x521B;&#x5EFA;tornado.web.Application&#x7684;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F20;&#x5165;&#x4E86;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x2014;&#x2014;&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x5217;&#x8868;&#x3002;&#x5B9E;&#x9645;&#x4E0A;Application&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8FD8;&#x63A5;&#x6536;&#x5F88;&#x591A;&#x5173;&#x4E8E;tornado web&#x5E94;&#x7528;&#x7684;&#x914D;&#x7F6E;&#x53C2;&#x6570;&#xFF0C;&#x5728;&#x540E;&#x9762;&#x7684;&#x5B66;&#x4E60;&#x4E2D;&#x6211;&#x4EEC;&#x7528;&#x5230;&#x7684;&#x5730;&#x65B9;&#x4F1A;&#x4E3A;&#x5927;&#x5BB6;&#x4ECB;&#x7ECD;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5148;&#x6765;&#x5B66;&#x4E60;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<p><strong>debug</strong>&#xFF0C;&#x8BBE;&#x7F6E;tornado&#x662F;&#x5426;&#x5DE5;&#x4F5C;&#x5728;&#x8C03;&#x8BD5;&#x6A21;&#x5F0F;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;False&#x5373;&#x5DE5;&#x4F5C;&#x5728;&#x751F;&#x4EA7;&#x6A21;&#x5F0F;&#x3002;&#x5F53;&#x8BBE;&#x7F6E;debug=True
&#x540E;&#xFF0C;tornado&#x4F1A;&#x5DE5;&#x4F5C;&#x5728;&#x8C03;&#x8BD5;/&#x5F00;&#x53D1;&#x6A21;&#x5F0F;&#xFF0C;&#x5728;&#x6B64;&#x79CD;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;tornado&#x4E3A;&#x65B9;&#x4FBF;&#x6211;&#x4EEC;&#x5F00;&#x53D1;&#x800C;&#x63D0;&#x4F9B;&#x4E86;&#x51E0;&#x79CD;&#x7279;&#x6027;&#xFF1A;</p>
<ul>
<li><strong>&#x81EA;&#x52A8;&#x91CD;&#x542F;</strong>&#xFF0C;tornado&#x5E94;&#x7528;&#x4F1A;&#x76D1;&#x63A7;&#x6211;&#x4EEC;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x6587;&#x4EF6;&#xFF0C;&#x5F53;&#x6709;&#x6539;&#x52A8;&#x4FDD;&#x5B58;&#x540E;&#x4FBF;&#x4F1A;&#x91CD;&#x542F;&#x7A0B;&#x5E8F;&#xFF0C;&#x8FD9;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x6211;&#x4EEC;&#x624B;&#x52A8;&#x91CD;&#x542F;&#x7A0B;&#x5E8F;&#x7684;&#x6B21;&#x6570;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x4E00;&#x65E6;&#x6211;&#x4EEC;&#x4FDD;&#x5B58;&#x7684;&#x66F4;&#x6539;&#x6709;&#x9519;&#x8BEF;&#xFF0C;&#x81EA;&#x52A8;&#x91CD;&#x542F;&#x4F1A;&#x5BFC;&#x81F4;&#x7A0B;&#x5E8F;&#x62A5;&#x9519;&#x800C;&#x9000;&#x51FA;&#xFF0C;&#x4ECE;&#x800C;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x4FDD;&#x5B58;&#x4FEE;&#x6B63;&#x9519;&#x8BEF;&#x540E;&#x624B;&#x52A8;&#x542F;&#x52A8;&#x7A0B;&#x5E8F;&#x3002;&#x8FD9;&#x4E00;&#x7279;&#x6027;&#x4E5F;&#x53EF;&#x5355;&#x72EC;&#x901A;&#x8FC7;autoreload=True&#x8BBE;&#x7F6E;&#xFF1B;</li>
<li><strong>&#x53D6;&#x6D88;&#x7F13;&#x5B58;&#x7F16;&#x8BD1;&#x7684;&#x6A21;&#x677F;</strong>&#xFF0C;&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x901A;&#x8FC7;compiled_template_cache=False&#x6765;&#x8BBE;&#x7F6E;&#xFF1B;</li>
<li><strong>&#x53D6;&#x6D88;&#x7F13;&#x5B58;&#x9759;&#x6001;&#x6587;&#x4EF6;hash&#x503C;</strong>&#xFF0C;&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x901A;&#x8FC7;static_hash_cache=False&#x6765;&#x8BBE;&#x7F6E;&#xFF1B;</li>
<li><strong>&#x63D0;&#x4F9B;&#x8FFD;&#x8E2A;&#x4FE1;&#x606F;</strong>&#xFF0C;&#x5F53;RequestHandler&#x6216;&#x8005;&#x5176;&#x5B50;&#x7C7B;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x800C;&#x672A;&#x88AB;&#x6355;&#x83B7;&#x540E;&#xFF0C;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x8FFD;&#x8E2A;&#x4FE1;&#x606F;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x901A;&#x8FC7;serve_traceback=True&#x6765;&#x8BBE;&#x7F6E;&#x3002;</li>
</ul>
<p>&#x4F7F;&#x7528;debug&#x53C2;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tornado.web
app = tornado.web.Application([], debug=<span class="hljs-keyword">True</span>)
</code></pre>
<h2 id="&#x8DEF;&#x7531;&#x6620;&#x5C04;">&#x8DEF;&#x7531;&#x6620;&#x5C04;</h2>
<p>&#x5148;&#x524D;&#x6211;&#x4EEC;&#x5728;&#x6784;&#x5EFA;&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x5217;&#x8868;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x4E8C;&#x5143;&#x5143;&#x7EC4;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python">[(<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),]
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x8FD9;&#x4E2A;&#x6620;&#x5C04;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x8DEF;&#x7531;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x8FD8;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x591A;&#x4E2A;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python">[
    (<span class="hljs-string">r&quot;/&quot;</span>, Indexhandler),
    (<span class="hljs-string">r&quot;/cpp&quot;</span>, ItcastHandler, {<span class="hljs-string">&quot;subject&quot;</span>:<span class="hljs-string">&quot;c++&quot;</span>}),
    url(<span class="hljs-string">r&quot;/python&quot;</span>, ItcastHandler, {<span class="hljs-string">&quot;subject&quot;</span>:<span class="hljs-string">&quot;python&quot;</span>}, name=<span class="hljs-string">&quot;python_url&quot;</span>)
]
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x8DEF;&#x7531;&#x4E2D;&#x7684;&#x5B57;&#x5178;&#xFF0C;&#x4F1A;&#x4F20;&#x5165;&#x5230;&#x5BF9;&#x5E94;&#x7684;RequestHandler&#x7684;initialize()&#x65B9;&#x6CD5;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> tornado.web <span class="hljs-keyword">import</span> RequestHandler
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ItcastHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">initialize</span><span class="hljs-params">(self, subject)</span>:</span>
        self.subject = subject

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(self.subject)
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x8DEF;&#x7531;&#x4E2D;&#x7684;name&#x5B57;&#x6BB5;&#xFF0C;&#x6CE8;&#x610F;&#x6B64;&#x65F6;&#x4E0D;&#x80FD;&#x518D;&#x4F7F;&#x7528;&#x5143;&#x7EC4;&#xFF0C;&#x800C;&#x5E94;&#x4F7F;&#x7528;tornado.web.url&#x6765;&#x6784;&#x5EFA;&#x3002;name&#x662F;&#x7ED9;&#x8BE5;&#x8DEF;&#x7531;&#x8D77;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8C03;&#x7528;RequestHandler.reverse_url(name)&#x6765;&#x83B7;&#x53D6;&#x8BE5;&#x540D;&#x5B50;&#x5BF9;&#x5E94;&#x7684;url&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">import</span> tornado.options
<span class="hljs-keyword">from</span> tornado.options <span class="hljs-keyword">import</span> options, define
<span class="hljs-keyword">from</span> tornado.web <span class="hljs-keyword">import</span> url, RequestHandler

define(<span class="hljs-string">&quot;port&quot;</span>, default=<span class="hljs-number">8000</span>, type=int, help=<span class="hljs-string">&quot;run server on the given port.&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        python_url = self.reverse_url(<span class="hljs-string">&quot;python_url&quot;</span>)
        self.write(<span class="hljs-string">&apos;&lt;a href=&quot;%s&quot;&gt;itcast&lt;/a&gt;&apos;</span> %
                   python_url)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ItcastHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">initialize</span><span class="hljs-params">(self, subject)</span>:</span>
        self.subject = subject

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(self.subject)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    tornado.options.parse_command_line()
    app = tornado.web.Application([
            (<span class="hljs-string">r&quot;/&quot;</span>, Indexhandler),
            (<span class="hljs-string">r&quot;/cpp&quot;</span>, ItcastHandler, {<span class="hljs-string">&quot;subject&quot;</span>:<span class="hljs-string">&quot;c++&quot;</span>}),
            url(<span class="hljs-string">r&quot;/python&quot;</span>, ItcastHandler, {<span class="hljs-string">&quot;subject&quot;</span>:<span class="hljs-string">&quot;python&quot;</span>}, name=<span class="hljs-string">&quot;python_url&quot;</span>)
        ],
        debug = <span class="hljs-keyword">True</span>)
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c03/index.html" class="navigation navigation-prev " aria-label="Previous page: 3 深入Tornado"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c03/s02.html" class="navigation navigation-next " aria-label="Next page: 3.2 输入"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
